package com.penghaisoft.microwms.material.web.intowarehouse.controller;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.controller.BaseController;
import com.penghaisoft.microwms.common.dto.TransResult;
import com.penghaisoft.microwms.common.page.Pager;
import com.penghaisoft.microwms.common.service.IWmsCommonService;
import com.penghaisoft.microwms.common.wms.model.RawBarcode;
import com.penghaisoft.microwms.common.wms.model.RawStorageSn;
import com.penghaisoft.microwms.material.web.intowarehouse.service.IMaterialIntoService;

/**
 * @Description 原材料条码入库
 * @author luoteng
 * @date 2018年5月23日 下午5:26:31
 * @version 1.0
 */
@RestController
@RequestMapping(value = "intowarehouse")
@Slf4j
public class MaterialIntoController extends BaseController {

	@Autowired
	private IMaterialIntoService materialIntoService;

	@Autowired
	private IWmsCommonService wmsCommonService;

	/**
	 * @Title list
	 * @Description 原材料条码查询列表
	 * @author luoteng
	 * @date 2018年5月23日:下午2:22:46
	 * @param page
	 * @param rows
	 * @param rawBarcode
	 * @return TransResult
	 */
	@PostMapping("barcode/list")
	public TransResult listBarcode(
			@RequestParam(name = "page", defaultValue = "1") int page,
			@RequestParam(name = "rows", defaultValue = "10") int rows,
			RawBarcode rawBarcode) {
		Pager<RawBarcode> result = materialIntoService.findListByCondition(
				page, rows, rawBarcode);
		return success(result);
	}

	/**
	 * @Title edit
	 * @Description 条码修改、新增
	 * @author luoteng
	 * @date 2018年5月23日:下午5:32:16
	 * @param rawBarcode
	 * @return TransResult
	 */
	@PostMapping("barcode/edit")
	public TransResult editBarcode(RawBarcode rawBarcode) {
		TransResult result = new TransResult();
		Date now = new Date();
		if(rawBarcode.getModelType() != null){
			if(rawBarcode.getModelType().equals(Constant.MODEL_WT)
					|| rawBarcode.getModelType().equals(Constant.MODEL_ST) 
					|| rawBarcode.getModelType().equals(Constant.MODEL_BJ)
					|| rawBarcode.getModelType().equals(Constant.MODEL_GD)){
				if(rawBarcode.getAmount() == null){
					result.setCode(Constant.FAIL);
					result.setMsg("请输入计量值！");
					return result;
				}else if(rawBarcode.getAmount().compareTo(new BigDecimal(1)) != 0){
					result.setCode(Constant.FAIL);
					result.setMsg("管件的计量值只能为1！");
					return result;
				}
			}else if(rawBarcode.getModelType().equals(Constant.MODEL_B) || rawBarcode.getModelType().equals(Constant.MODEL_H)){
				result.setCode(Constant.FAIL);
				result.setMsg("黑白料不能生成条码！");
				return result;
			}
		}
		if (rawBarcode.getId() == null || "".equals(rawBarcode.getId())) {
			if (rawBarcode.getProductNum() != null
					&& Integer.parseInt(rawBarcode.getProductNum()) > 0) {
				List<String> barcodeList = wmsCommonService.getProBarCodeShortSeq5(rawBarcode.getModelType(), rawBarcode.getModelType(),
						Integer.parseInt(rawBarcode.getProductNum()));
				
				rawBarcode.setActiveFlag("1");
				// 状态 0：未打印 1：已打印
				rawBarcode.setStatus("0");
				// 入库状态：0 未入库 1已入库
				rawBarcode.setUserDefined1("0");
				rawBarcode.setCreateBy(getLoginName());
				rawBarcode.setGmtCreate(now);
//				materialIntoService.createBarcode(rawBarcode);
				materialIntoService.createBarcodeByList(rawBarcode, barcodeList);
			} else {
				result.setCode(Constant.FAIL);
				result.setMsg("请输入生成条码个数！");
				return result;
			}
		} else {
			rawBarcode.setLastModifiedBy(getLoginName());
			rawBarcode.setGmtModified(now);
			materialIntoService.updateBarcodeRelate(rawBarcode);
		}
		return success();
	}

	/**
	 * @Title get
	 * @Description 根据ID获取条码信息
	 * @author luoteng
	 * @date 2018年5月23日:下午5:55:34
	 * @param rawBarcode
	 * @return TransResult
	 */
	@GetMapping("barcode/get/{id}")
	public TransResult getBarcode(@PathVariable("id") String id) {
		RawBarcode rtnOb = new RawBarcode();
		if (id != null && !"".equals(id)) {
			RawBarcode ob = new RawBarcode();
			ob.setId(Integer.parseInt(id));
			List<RawBarcode> list = materialIntoService.getBarcodeList(ob);
			if (list != null && !list.isEmpty()) {
				rtnOb = list.get(0);
			}
		}
		return success(rtnOb);
	}

	/**
	 * @Title delBarcode
	 * @Description 删除条码
	 * @author luoteng
	 * @date 2018年5月24日:下午2:59:43
	 * @param ob
	 * @return TransResult
	 */
	@PostMapping("barcode/d")
	public TransResult delBarcode(RawBarcode ob) {
		Date now = new Date();
		// 有效标志 1-有效 0-无效
		ob.setActiveFlag("0");
		ob.setLastModifiedBy(getLoginName());
		ob.setGmtModified(now);
		materialIntoService.updateBarcodeRelate(ob);
		return success();
	}

	@PostMapping("barcode/intowh/list")
	public TransResult listBarcodeIntoWh(
			@RequestParam(name = "page", defaultValue = "1") int page,
			@RequestParam(name = "rows", defaultValue = "10") int rows,
			RawStorageSn rawStorageSn) {
		Pager<RawStorageSn> result = materialIntoService
				.findIntoWhRawStorageSnList(page, rows, rawStorageSn);
		return success(result);
	}
}
